package com.atguigu.service.impl;

import com.atguigu.cache.ShopCache;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuImage;
import com.atguigu.entity.SkuInfo;
import com.atguigu.mapper.ProductSalePropertyKeyMapper;
import com.atguigu.mapper.ProductSalePropertyValueMapper;
import com.atguigu.mapper.SkuSalePropertyValueMapper;
import com.atguigu.service.SkuDetailService;
import com.atguigu.service.SkuImageService;
import com.atguigu.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SkuDetailServiceImpl implements SkuDetailService {
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    private SkuImageService skuImageService;
    @Autowired
    ProductSalePropertyKeyMapper salePropertyKeyMapper;
    @Autowired
    SkuSalePropertyValueMapper skuSalePropertyValueMapper;

    @Override
    @ShopCache(prefix = "sku:info:")
    public SkuInfo getSkuInfo(Long skuId) {
        //查询skuInfo的信息
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        if(skuInfo!=null){
        //查询skuInfo对应的图片信息
        QueryWrapper<SkuImage> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id",skuId);
        List<SkuImage> list = skuImageService.list(wrapper);
        skuInfo.setSkuImageList(list);
        }
        return skuInfo;
    }

    @Override
    public List<ProductSalePropertyKey> getSpuSalePropertyAndSelected(Long productId,Long skuId) {

        return salePropertyKeyMapper.getSpuSalePropertyAndSelected(productId,skuId);
    }

    @Override
    @ShopCache(prefix = "sku:idMapping:")
    public Map getSalePropertyAndSkuIdMapping(Long productId) {
        Map<Object, Object> hashMap = new HashMap();
        List<Map> valueIdMap= skuSalePropertyValueMapper.getSalePropertyAndSkuIdMapping(productId);
        if (!CollectionUtils.isEmpty(valueIdMap)){
            for (Map map : valueIdMap) {
                hashMap.put(map.get("sale_property_value_id"),map.get("sku_id"));
            }
        }
        return hashMap;
    }
}
